<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
     public function login(){//显示登录页面
        $this->display();
    }
     public function code(){
         $config= array(
             'fontSize'  =>  18,// 验证码字体
              'imageW'  =>  130,// 验证码宽120
              'imageH'  => 35,// 验证码高35
              'length' => 4, // 验证码位数
              'useNoise' => false,// 关闭验证码杂点
         );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }
     public function dologin(){
        if(IS_POST){//检查验证码
        $rst = $this->checkcode(I('post.code'));
        if($rst===false)
        $this->error('验证失败');
        $username=I('post.username');
        $password=I('post.password');
        $User=M("tuser");
        $userdata=$User->where("username='$username'")->find();
        if($userdata!=null)
            {
            $md5password=md5($password);
            if($md5password==$userdata['password'])
              {
                session('loginname',$username);
            $this->success('登录成功，请稍等',U('Admin/Index/index'));
            }
            else {
            $this->error('登录失败，密码错误');
            }
        }    
        else{
            $this->error('登录失败，用户名不存在');
            }
        }
  
     }
     public function checkcode($code,$codeid=''){
          $Verify = new\Think\Verify();
         return  $Verify->check($code,$codeid);
     }
     public function logout(){                      //退出登录
         session('[destroy]');
         $this->success('退出成功',U('Admin/Index/index'));
     }
}